package org.jsping.loginservlet;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jsping.entity.User;
import org.jsping.service.ILoginUserService;
import org.jsping.service.impl.LoginUserServiceImpl;
import org.jsping.util.AuthUtil;

import com.alibaba.fastjson.JSONObject;

@WebServlet("/QQcallBack.do")
public class QQCallBackServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	//用户登录/注册业务处理
	private ILoginUserService loginUser = new LoginUserServiceImpl();
	
	/**
	 * 调用qq的接口,处理用户qq登录的请求
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String code = req.getParameter("code");
		System.out.println(code); 		
		//获取token
		String backUrl ="http://ping.xn--fiqs8s/QQcallBack.do";
		String url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code"
				+ "&client_id="+AuthUtil.QQAPPID
				+ "&client_secret="+AuthUtil.QQAPPSECRET
				+ "&code="+code
				+ "&redirect_uri="+URLEncoder.encode(backUrl);
		String info = AuthUtil.doGetString(url);
		System.out.println(info);
		
		String token = info.substring(info.indexOf("=")+1, info.indexOf("&"));
		System.out.println(token);
		//获取open
		String openidUrl = "https://graph.qq.com/oauth2.0/me?access_token="+token;
		String callback = AuthUtil.doGetString(openidUrl);
//		String open = callback.getString("openid");
		System.out.println(callback);
		
		callback = callback.replace("callback(", " ");
		callback = callback.replace(");", " ");
		JSONObject jobj = JSONObject.parseObject(callback);

		String opid = jobj.getString("openid");
		System.out.println(opid);
		
		String userinfoUrl = "https://graph.qq.com/user/get_user_info?"
				+ "access_token="+token
				+ "&oauth_consumer_key="+AuthUtil.QQAPPID
				+ "&openid="+opid;
		
		JSONObject uinfo = AuthUtil.doGetJson(userinfoUrl);
		//增加QQ用户
		//qq性别返回的是男/女，所以需要判断,男==1，女==2，默认0(未知)
		User u = new User(uinfo.getString("nickname"),opid,uinfo.getString("gender")=="男"?1:uinfo.getString("gender")=="女"?2:0,uinfo.getString("figureurl_qq_1"));
		//返回布尔值...?有什么用？
		loginUser.addOpenidUser(u);
		
		User newsU = loginUser.obtain(u.getOpenid());
		//昵称
		Cookie uname = new Cookie("uname", uinfo.getString("nickname"));
		//头像
		Cookie uimg = new Cookie("img", uinfo.getString("figureurl_qq_1"));
		//性别
		Cookie usex = new Cookie("sex", uinfo.getString("gender"));
		
		Cookie uid = new Cookie("uid",newsU.getUid()+"");
		
		resp.addCookie(uname);
		resp.addCookie(uimg);
		resp.addCookie(usex);
		resp.addCookie(uid);
		
		HttpSession session = req.getSession();
		session.setAttribute("user", "qq");
		resp.sendRedirect("http://ping.xn--fiqs8s");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}
}
